<?php
require_once("../includes/bootstrap.php");

function login(){	
	$username = $_REQUEST['username'];
	$password = $_REQUEST['password'];
	if($username!= "" && $password != ""){		
		global $conn;
		$query = " CALL Logeo_Usuario_sp(?,?)";
		$params = array($username,$password);
		try{
			$result = $conn->Execute($query,$params);			
			
			if(isset($result->fields))
			if($result->fields[0] > 0) {
//1,`Id_U`,`U_Nick`,`U_Nombre`,`U_ApellidoP`,`U_ApellidoM`,`U_Correo`,`U_Punto`,`U_Sexo`,`U_Edad`,`U_Instituto`,`U_Fecha`
				$_SESSION['type'] = $result->fields[0] ;
				$_SESSION['id'] = $result->fields["Id_U"];
				$_SESSION['username'] = $username;
				$_SESSION['name'] = $result->fields[3];
				$_SESSION['apellidop'] = $result->fields[4];
				$_SESSION['apellidom'] = $result->fields[5];
				$_SESSION['mail'] = $result->fields[6];
				$_SESSION['puntos'] = $result->fields[7];
				$_SESSION['sexo'] = $result->fields[8];
				$_SESSION['edad'] = $result->fields[9];
				$_SESSION['instituto'] = $result->fields[10];
				$_SESSION['fecha'] = $result->fields[11];
				
				echo '{"status":"ok","error":""}'; 
				return;				
			}else{
				echo '{"status":"error","error":"Datos incorrectos."}';
			}
		}
		catch(Exception $e){ echo '{"status":"error","error":"ups algo salio mal"}';}
	}else{ echo '{"status":"error","error":"campos vacios"}'; }
}


function logout(){
	session_destroy();
	echo '{"status":"ok","error":""}'; 
}

function checksession(){
	if(isset($_SESSION['type'])){
			echo '{"status":"ok","error":"","username":"'.$_SESSION['username'].'","type":"'.$_SESSION['type'].'"}';
			return;
	}
	echo '{"status":"error","error":"Usuario no logeado"}';
}

function register(){
	//Registro_Usuario_sp( U_nick varchar( 20 ) , U_contrasena varchar( 20 ) , U_Correo varchar( 50 ) ) 
	$username = $_REQUEST['username'];
	$password = $_REQUEST['password'];
	$mail = $_REQUEST['mail'];
	if($username!= "" && $password != "" && $mail != ""){		
		global $conn;
		$query = " CALL Registro_Usuario_sp( ?, ? , ? ) ";
		$params = array($username,$password,$mail);
		try{
			$result = $conn->Execute($query,$params);
			//$rs = mysql_fetch_array($result);
			//echo $result->fields[0]."fields";
			if(isset($result->fields[0]))
			if($result->fields[0] > 0) {						
				echo '{"status":"ok","error":""}'; 
				return;				
			}else{
				echo '{"status":"error","error":"El nombre de usuario ya existe."}';
			}
		}
		catch(Exception $e){ echo '{"status":"error","error":"ups algo salio mal"}';}
	}else{ echo '{"status":"error","error":"campos vacios"}'; }
}

if(isset($_REQUEST['action']))
	switch($_REQUEST['action']){
		// iniciar sesion
		case 1:
			login();
		break;
		//cerrar sesion
		case 2:
			logout();
		break;
		//esta logeado?
		case 3:
			checksession();
		break;
		//resitrar al usuario
		case 4:
			register();
		break;
	}
else{
	echo '{"status":"error","error":"faltan parametros"}'; return;
	}
?>